<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;


/**
 * 代理商发布的内容 后台审核
 */
class ExamineController extends Controller
{

	/**
	 *待审核列表
	 *
	 */
	public function list(Request $request)
    {
        return $this->response->array(
            DB::table('materials')
                ->orderByDesc('created_at')
                ->paginate());
    }

    /**
     * 待审核详细内容
     * 
     */
    public function info($id)
    {
        return $this->response->array(
            DB::table('materials')
                ->findOrFail($id)
                ->toArray()
        );
    }

    /**
     * 通过审核
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     */
    public function agree(Request $request)
    {
        try {
            DB::table('materials')
                ->where('id', $request->get('id'))
                ->update([
                    'status' => 2
                ]);
        } catch (\Exception $e) {
            $this->response->errorBadRequest($e->getMessage());
        }

        return $this->response->created(null);
    }

    /**
     * 没通过审核
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     */
    public function notAgree(Request $request)
    {
        try {
            DB::table('materials')
                ->where('id', $request->get('id'))
                ->update([
                    'audit_result' => $request->get('remark', ''),
                    'status' => 3
                ]);
        } catch (\Exception $e) {
            $this->response->errorBadRequest($e->getMessage());
        }

        return $this->response->created(null);
    }
}